<?php namespace Home\Controller;

//收藏夹控制器
class CollectionController extends CommonController{
	protected $model;
	public function __auto(){
		//实例化收藏夹表
		$this->model=new \Common\Model\UserJob;
	}

	//收藏夹首页
	public function index(){
		$uid=$_SESSION['homeInfo']['uid'];
		$data=$this->model
			->join('job','jid','=','lg_job_jid')
			->where(C('database.prefix')."user_job.lg_user_uid=$uid")
			->get();
		$cityModel=new \Common\Model\City;
		$companyModel=new \Common\Model\Company;
		$statusModel=new \Common\Model\Status;
		foreach ($data as $key => $value) {
			//压入公司信息
			$companyData=$companyModel->where("gsid=".$data[$key]['lg_company_gsid'])->find();
			$data[$key]['logo']=$companyData['logo'];
			$data[$key]['gsid']=$companyData['gsid'];
			$data[$key]['gsname']=$companyData['gsname'];
			//获得所属城市
			$pid=$cityModel->where("city_id=".$data[$key]['lg_city_city_id'])->pluck('city_pid');
			$city=$cityModel->where("city_id={$pid}")->pluck('city_name');
			$data[$key]['city']=$city;
			//判断是否投递了该职位
			$deliver=$statusModel->where("lg_job_jid={$data[$key]['jid']} AND lg_user_uid={$_SESSION['homeInfo']['uid']}")->find();
			if ($deliver){
				$data[$key]['deliver']=1;
			}else{
				$data[$key]['deliver']=0;
			}
		}
		View::with('data',$data);
		View::make();
	}

	//收藏职位
	public function collect(){
		if (!$this->model->store()) {
			echo 0;
		}else{
			echo 1;
		}
		die;
	}

	//取消收藏
	public function unCollect(){
		if (!$this->model->del()) {
			echo 0;
		}else{
			echo 1;
		}
		die;
	}

	//收藏夹中的取消收藏
	public function delCollect(){
		if (!$this->model->del()) View::error('您并没有收藏该职位');
		View::success('取消收藏成功');
	}

}